package cn.huapu.power.energy.cfg;

import org.ehcache.jsr107.EhcacheCachingProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.ResourceUtils;

import java.io.FileNotFoundException;
import java.net.URI;
import java.net.URISyntaxException;

//import org.springframework.data.redis.core.RedisTemplate;  
//import org.springframework.data.redis.serializer.StringRedisSerializer;  

/**
 * 缓存配置
 * @author hunter
 *
 */
@Configuration
@EnableCaching
public class CacheConfig {	
	@Bean(name="cacheManager") 
	public CacheManager cacheManager() throws URISyntaxException, FileNotFoundException {		
		return new JCacheCacheManager(jCacheManager()); 
	}
	@Bean(name="jCacheManager")
	public javax.cache.CacheManager jCacheManager() throws URISyntaxException, FileNotFoundException { 
		//ehcache实现了javax的CachingProvider接口的具体实现 
		EhcacheCachingProvider ehcacheCachingProvider = new EhcacheCachingProvider();
		//根据配置文件获取cachemanager 
		URI uri = ResourceUtils.getURL("classpath:config/cache/ehcache.xml").toURI();
		javax.cache.CacheManager cacheManager = ehcacheCachingProvider.getCacheManager(uri,this.getClass().getClassLoader());			
		return cacheManager;
	}
	
	@Autowired
    private RedisConnectionFactory redisConnectionFactory;
    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        // 设置redis主键的序列化形式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }


    @Bean
    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForHash();
    }

    @Bean
    public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
        return redisTemplate.opsForValue();
    }

    @Bean
    public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForList();
    }

    @Bean
    public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForSet();
    }

    @Bean
    public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForZSet();
    }
    
    /**@Bean
    public JedisCluster jedisCluster(){ 
    	String [] serverArray="127.0.0.1:6379".split(",");
		Set<HostAndPort> nodes = new HashSet<>();
		for (String ipPort : serverArray) {
			String[] ipPortPair = ipPort.split(":");
			nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));

		}
		return new JedisCluster(nodes, 5000);
    }*/

}
